﻿#include "OiPCH.hpp"

namespace Oi
{
    namespace PLScannerCore
    {
        class MotorImpl : public Impl<Motor>
        {
        public:
            void init();
            void exit();

        };

        void MotorImpl::init()
        {
            OI_Q(Motor);
            ModbusCommTask::instance()->start();
        }

        void MotorImpl::exit()
        {
            ModbusCommTask::instance()->exit();
        }

        Motor::Motor()
        {
            OI_I(Motor);
        }

        Motor::~Motor()
        {
            OI_E(Motor);
        }

        void Motor::init()
        {
            OI_IMPL()->init();
        }

        void Motor::exit()
        {
            OI_IMPL()->exit();
        }

        bool Motor::isReady()
        {
            ushort status = axisStatus();

            return (status & AS_HOME_READY) && !(status & AS_ALARM);
        }

        bool Motor::isHome()
        {
            ushort status = axisStatus();
            return status & AS_HOME_READY;
        }

        bool Motor::isAlarm()
        {
            ushort status = axisStatus();
            return status & AS_ALARM;
        }

        bool Motor::loopModeReady()
        {
            ushort status = axisStatus();

            ushort command = 0;

            ModbusCommTask::instance()->read(2, 1, &command);

            return (status & AS_LOOP_READY) && (command == 0);
        }

        bool Motor::locatedModeReady()
        {
            ushort status = axisStatus();

            return (status & AS_LOCATED_READY);
        }

        bool Motor::axisMoveForward()
        {
            ushort status = axisStatus();

            return (status & AS_AXIS_DIR);
        }

        double Motor::axisPos() const
        {
            ushort pos;
            ModbusCommTask::instance()->read(4, 1, & pos);

            return 0.1 * (short)(pos);
        }

        ushort Motor::axisStatus() const
        {
            ushort status;
            ModbusCommTask::instance()->read(1, 1, &status);

            return status;
        }

        bool Motor::switchEnable() const
        {
            ushort appendix;
            ModbusCommTask::instance()->read(9, 1, &appendix);

            return  (appendix & AI_SWITCH_ENABLE);
        }

        bool Motor::changeFormualReady()
        {
            ushort status = 0;
            ModbusCommTask::instance()->readSignal(2036, 1, &status);
            return status;
        }

        void Motor::loopRun(double vel /*= 0.0*/, double from /*= 0.0*/, double end /*= 0.0*/)
        {
            short mVel = vel * 10;
            short mFrom = from * 10;
            short mEnd = end * 10;
            QVector<ushort> data;

            data << mVel << mFrom << mEnd;

            ModbusCommTask::instance()->write(5, 3, data.data());

            ushort command = 1;
            ModbusCommTask::instance()->write(2, 1, &command);
        }

        void Motor::locatedRun(double pos /*= 0.0*/)
        {
            ushort mPos = (short)(pos * 10);
            ModbusCommTask::instance()->write(3, 1, &mPos);
        }

        void Motor::loopMode()
        {
            ushort command = 1;
            ModbusCommTask::instance()->write(0, 1, &command);
        }

        void Motor::locateMode()
        {
            ushort command = 2;
            ModbusCommTask::instance()->write(0, 1, &command);
        }

        void Motor::home()
        {
            ushort command = 3;
            ModbusCommTask::instance()->write(0, 1, &command);
        }

        void Motor::stopRun()
        {
            ushort command = 0;
            ModbusCommTask::instance()->write(0, 1, &command);
        }

        void Motor::setResult(int r)
        {
            ushort v = r;
            ModbusCommTask::instance()->write(8, 1, &v);
        }
        void Motor::reChangeFormualStatus(int r)
        {
            ushort v = r;
            ModbusCommTask::instance()->write(10, 1, &v);
        }
        Motor* Motor::instance()
        {
            static Motor s_motor;

            return &s_motor;
        }

    }
}